//
// Created by harrytsz on 2021/1/3.
//

#ifndef DATASTRUCTURES_ARRAYLIST_H
#define DATASTRUCTURES_ARRAYLIST_H

template <class T>
class arrayList : public linearList<T> {
    public:
        // 构造函数，复制构造函数和析构函数
        arrayList(int initialCapacity = 10);
        arrayList(const arrayList<T>&);
        ~arrayList() {delete [] element;}

        // ADT 方法
        bool empty() const { return listSize == 0;}
        int size() const { return listSize;}
        T& get(int theIndex) const;
        int indexOf(const T& theElement) const;
        void erase(int theIndex);
        void insert(int theIndex, const T& theElement);
        void output(ostream& out) const;

        // 其他方法
        int capacity() const {return arrayLength;}

    protected:
        // 若索引 theIndex 无效，则抛出异常
        void checkIndex(int theIndex) const;

        // 存储线性表元素的一维数组
        T* element;

        // 一维数组的容量
        int arrayLength;

        // 线性表的元素个数
        int listSize;
};

template<class T>
arrayList<T>::arrayList(int initialCapacity) {
    // 构造函数
    if(initialCapacity < 1) {
        ostringstreams s;
        s << "Initial capacity = " << initialCapacity << " Must be > 0";
        throw illegalParameterValues(s.str());
    }

    arrayLength = initialCapacity;
    element = new T[arrayLength];
    listSize = 0;
}

template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList) {
    // 拷贝构造函数
    arrayLength = theList.arrayLength;
    listSize = theList.listSize;
    element = new T[arrayLength];
    copy(theList.element, theList.element + listSize, element);
}

#endif //DATASTRUCTURES_ARRAYLIST_H
